<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | create by allen.qiang
// +----------------------------------------------------------------------

/**
 * 权限ajax类
 * @package  ajax
 * @subpackage  backend
 * @author   create by allen.qiang
 */

quanli_require('module/base/project/permission.class.php');

class ajax_permission {
	
	public function execute(){

        if (isset($_REQUEST['operFlg']) && is_numeric($_REQUEST['operFlg']) && $_REQUEST['operFlg'] > 0) {

            switch ($_REQUEST['operFlg']) {

                case 1:
                    $this->searchPermissionList();//权限列表
                    break;

                case 2:
                    $this->searchPermission();//查询权限详情信息
                    break;

                case 100:

                    if (!isset($_REQUEST['act']) || !in_array($_REQUEST['act'], array('del', 'edit'))) {
                        returnJsonData('参数错误');
                    }
                    if ($_REQUEST['act'] == 'edit') {

                        $this->editPermission();//修改、新增权限

                    } else if ($_REQUEST['act'] == 'del') {

                        $this->deletePermission();//删除

                    }

                    break;

                default:
                    returnJsonData('参数错误');
                    break;
            }

        } else {
            returnJsonData('参数错误');
        }
		
	}

    /**
     * 查询权限详情信息
     */
    private function searchPermission(){

        global $G_PGLOB;

        if (!isset($_REQUEST['permissionID']) || !quanli_is_int($_REQUEST['permissionID'])) {
            returnJsonData('参数错误');
        }

        //实例权限类
        $permissionObj = new permission( $G_PGLOB->DBCA );

        //查询权限信息
        $arg_array = array(
            'permissionID' => $_REQUEST['permissionID']
        );
        $permission_array = $permissionObj->searchPermission( $arg_array );

        echo json_encode($permission_array[0]);

    }


    /**
     * 权限列表
     */
    private function searchPermissionList(){
		
		global $G_PGLOB;

        //实例权限类
        $permissionObj = new permission( $G_PGLOB->DBCA );
		
		//查询权限信息
        $arg_array = $_REQUEST;
		$permission_array = $permissionObj->searchPermission( $arg_array );

		//页面输出数组
		$result_array = array(
            'page' => $arg_array['page'],
            'total' => $arg_array['count'],
            'rows' => array()
		);

        if ( $permission_array ) {

            foreach ($permission_array as &$permission){
                $result_array['rows'][] = array(
                    'id' => $permission['permissionID'],
                    'cell' => array(
                        '<a href="javascript:void(0);" onclick="permission.edit(' . $permission['permissionID'] . ');">修改</a>',
                        $permission['permissionID'],
                        $permission['permissionName'],
                        $permission['permissionGroupName'],
                        $permission['permissionRequest']
                    )
                );
            }

        }

		echo json_encode($result_array);
		
	}

    /**
     * 新增或修改权限
     */
    private function editPermission(){
		
		global $G_PGLOB;

        //实例权限类
        $permissionObj = new permission( $G_PGLOB->DBCA );

        //新增或修改权限
        $_REQUEST['operator'] = $G_PGLOB->currentUserID;
		$result_array = $permissionObj->editPermission( $_REQUEST );
		
		echo json_encode( $result_array );
			
	}

    /**
     * 删除权限
     */
    private function deletePermission() {

        if (!isset($_REQUEST['p_array']) || empty($_REQUEST['p_array'])) {
            returnJsonData('参数错误');
        }
		
		global $G_PGLOB;

        //实例权限类
        $permissionObj = new permission( $G_PGLOB->DBCA );

        //删除权限
        $permission_array = explode(',', $_REQUEST['p_array']);
        $result_array = $permissionObj->deletePermission( $permission_array );

        echo json_encode( $result_array );
		
	}
	
}